﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace SaveFuel.NumberSelector
{
    public partial class FuelPrice : PhoneApplicationPage
    {
        public FuelPrice()
        {
            InitializeComponent();

            InitializeSymbol();
            LoadDataBack();
        }

        private void LoadDataBack()
        {
            if (App.price != string.Empty)
            {
                string[] moneyArray = App.price.Split(Convert.ToChar(App.currencySeparator));
                int value = Convert.ToInt16(moneyArray[0]);
                string decimals = moneyArray[1];
                int[] cents = new int[3];

                for (int i = 0; i < decimals.Length; i++)
                {
                    cents[i] = Convert.ToInt32(decimals[i].ToString());
                }

                WriteDataToLoopingSelector(value, cents);
            }
        }

        private void WriteDataToLoopingSelector(int value, int[] cents)
        {
            this.CellMasterPrice.DataSource.SelectedItem = value;
            this.Cell1.DataSource.SelectedItem = cents[0];
            this.Cell2.DataSource.SelectedItem = cents[1];
            this.Cell3.DataSource.SelectedItem = cents[2];
        }

        private void InitializeSymbol()
        {
            this.currencySeparator.Text = App.currencySeparator;
            this.currencySymbol.Text = App.moneyUnit;

            this.CellMasterPrice.DataSource = new NumberSelector.NumbersDataSource() { Minimum = 0, Maximum = 2, SelectedItem = 1 };
        }

        private void doneClick_Click(object sender, EventArgs e)
        {            
            string result = this.CellMasterPrice.DataSource.SelectedItem.ToString() + App.currencySeparator +
                this.Cell1.DataSource.SelectedItem.ToString() + this.Cell2.DataSource.SelectedItem.ToString() + this.Cell3.DataSource.SelectedItem.ToString();
            App.price = result;

            NavigationService.GoBack();
        }

        private void cancelClick_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

    }
}